package com.jdew.thkj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jdew.thkj.R;
import com.jdew.thkj.bean.CollectionBean;
import com.jdew.thkj.onitemInterface.OnItemClickListener;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yangyang on 2017/9/18.
 */

public class CollectionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{
    private List<CollectionBean> dataList;
    private LayoutInflater layoutInflater;
    private View itemView;
    private Context context;
    private static final int HEAD_TYPE=00001;
    private static final int BODY_TYPE=00002;
    private static final int FOOT_TYPE=00003;
    private int headCount=0;//头部个数，后续可以自己拓展
    private int footCount=1;//尾部个数，后续可以自己拓展
    private OnItemClickListener mItemClickListener;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private String maType;
    public void setOnDelListener(onSwipeListener mOnDelListener) {
        this.mOnSwipeListener = mOnDelListener;
    }
    public interface onSwipeListener {
        void onDel(int pos);
    }

    private onSwipeListener mOnSwipeListener;

    public onSwipeListener getOnDelListener() {
        return mOnSwipeListener;
    }
    @Override
    public void onClick(View v) {
        if (mItemClickListener!=null){
            mItemClickListener.onItemClick((Integer) v.getTag());
        }
    }
    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }
    public CollectionAdapter(Context context, List<CollectionBean> dataList) {
        this.context=context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(context);
        sp = context.getSharedPreferences( "user", Activity.MODE_PRIVATE );
        editor = sp.edit();
        maType=sp.getString("SpareTwo","");
    }
    public void setDataList(List<CollectionBean> dataList){
        this.dataList=dataList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case BODY_TYPE:
                itemView = layoutInflater.inflate( R.layout.product_list_item, parent, false );
                itemView.setOnClickListener(this);
                return new MyViewHolder(itemView);
            case FOOT_TYPE:
                return new FootViewHolder( layoutInflater.inflate( R.layout.recycle_foot, parent, false ) );
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
         if (holder instanceof MyViewHolder) {
             final MyViewHolder myviewHolder=(MyViewHolder)holder;
             myviewHolder.BindView(holder,position);
             myviewHolder.itemView.setTag(position);
         }else if (holder instanceof FootViewHolder) {

        }
    }

    @Override
    public int getItemCount() {
        return dataList.size()+footCount;
    }

    private boolean isHead(int position) {
        return headCount!=0&&position<headCount;
    }

    private int getBodySize() {
        return dataList.size();
    }
    private boolean isFoot(int position) {
        return footCount!=0&&(position>=(getBodySize()+headCount));
    }

    @Override
    public int getItemViewType(int position) {
        if (isHead(position)) {
            return HEAD_TYPE;
        }else if (isFoot(position)) {
            return FOOT_TYPE;
        }else {
            return BODY_TYPE;
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.product)
        ImageView product;
        @Bind(R.id.source)
        ImageView source;
        @Bind(R.id.price)
        TextView price;
        @Bind(R.id.before_price)
        TextView before_price;
        @Bind(R.id.num)
        TextView num;
        @Bind(R.id.quan)
        TextView quan;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.earn)
        TextView earn;
        MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
        public void BindView(final RecyclerView.ViewHolder holder, int position){
            DecimalFormat df = new DecimalFormat( "0.00" );
            Glide.with( context ).load(dataList.get(position).getUrl()).into( product );
            ((MyViewHolder) holder).before_price.getPaint().setFlags( Paint. STRIKE_THRU_TEXT_FLAG );
            if(dataList.get( position ).getIdentification().equals("0")){
                source.setImageResource(R.mipmap.tb);
            }else{
                source.setImageResource(R.mipmap.tm);
            }
            ((MyViewHolder) holder).before_price.setText("原价"+df.format(Double.parseDouble(dataList.get( position).getPrice())));
            ((MyViewHolder) holder).price.setText(df.format(Double.parseDouble(dataList.get( position ).getPrice())-Double.parseDouble(dataList.get(position).getCoupon()))+"");
            ((MyViewHolder) holder).num.setText("月销:"+dataList.get(position).getVolume());
            ((MyViewHolder) holder).quan.setText(dataList.get(position).getCoupon().replace(".00","")+"元券");
            ((MyViewHolder) holder).name.setText(dataList.get(position).getTitle());
            if(maType.equals("")|| maType.equals("1")){
                earn.setVisibility(View.GONE);
            }else{
                earn.setVisibility(View.VISIBLE);
                ((MyViewHolder) holder).earn.setText(dataList.get(position).getCommission().replace("分享赚 ¥",""));
            }
        }
    }

    private static class FootViewHolder extends RecyclerView.ViewHolder {

        public FootViewHolder(View itemView) {
            super(itemView);
        }
    }

}
